import { Table, Input, Button, Icon,Divider ,Pagination } from 'antd';
import Highlighter from 'react-highlight-words';
import * as api from '../../util/api'

const res =  api.sortList()
  .then(res=>{
    console.log(res.data.categories)
    return res.data.categories
  })

const data = [
  {
      _id: "5dd8fc1be483b00b0c143538",
      name: "围巾",
      createdAt: "2019-11-23T09:30:03.667Z",
      updatedAt: "2019-11-23T09:30:03.667Z",
      __v: 0
  },
  {
      _id: "5dd8fc12e483b00b0c143537",
      name: "袜",
      createdAt: "2019-11-23T09:29:54.970Z",
      updatedAt: "2019-11-23T09:29:54.970Z",
      __v: 0
  },
  {
      _id: "5dd8fc0ae483b00b0c143536",
      name: "包",
      createdAt: "2019-11-23T09:29:46.493Z",
      updatedAt: "2019-11-23T09:29:46.493Z",
      __v: 0
  },
  {
      _id: "5dd8fc04e483b00b0c143535",
      name: "帽",
      createdAt: "2019-11-23T09:29:40.193Z",
      updatedAt: "2019-11-23T09:29:40.193Z",
      __v: 0
  },
  {
      _id: "5dd8fbfde483b00b0c143534",
      name: "项链",
      createdAt: "2019-11-23T09:29:33.347Z",
      updatedAt: "2019-11-23T09:29:33.347Z",
      __v: 0
  },
  {
      _id: "5dd8fbe3e483b00b0c143532",
      name: "休闲鞋",
      createdAt: "2019-11-23T09:29:07.105Z",
      updatedAt: "2019-11-23T09:29:07.105Z",
      __v: 0
  },
  {
      _id: "5dd8fbd8e483b00b0c143531",
      name: "凉鞋/拖鞋",
      createdAt: "2019-11-23T09:28:56.859Z",
      updatedAt: "2019-11-23T09:28:56.859Z",
      __v: 0
  },
  {
      _id: "5dd8fbbce483b00b0c143530",
      name: "棉衣",
      createdAt: "2019-11-23T09:28:28.043Z",
      updatedAt: "2019-11-23T09:28:28.043Z",
      __v: 0
  },
  {
      _id: "5dd8fbaee483b00b0c14352f",
      name: "羽绒服",
      createdAt: "2019-11-23T09:28:14.089Z",
      updatedAt: "2019-11-23T09:28:14.089Z",
      __v: 0
  },
  {
      _id: "5dd8fba9e483b00b0c14352e",
      name: "毛呢大衣",
      createdAt: "2019-11-23T09:28:09.241Z",
      updatedAt: "2019-11-23T09:28:09.241Z",
      __v: 0
  }
]

export default class App extends React.Component {
  state = {
    searchText: '',
  };

  getColumnSearchProps = dataIndex => ({
    filterDropdown: ({ setSelectedKeys, selectedKeys, confirm, clearFilters }) => (
      <div style={{ padding: 8 }}>
        <Input
          ref={node => {
            this.searchInput = node;
          }}
          placeholder={`查找 ${dataIndex}`}
          value={selectedKeys[0]}
          onChange={e => setSelectedKeys(e.target.value ? [e.target.value] : [])}
          onPressEnter={() => this.handleSearch(selectedKeys, confirm)}
          style={{ width: 188, marginBottom: 8, display: 'block' }}
        />
        <Button
          type="primary"
          onClick={() => this.handleSearch(selectedKeys, confirm)}
          icon="search"
          size="small"
          style={{ width: 90, marginRight: 8 }}
        >
          搜索
        </Button>
        <Button onClick={() => this.handleReset(clearFilters)} size="small" style={{ width: 90 }}>
          重置
        </Button>
      </div>
    ),
    filterIcon: filtered => (
      <Icon type="search" style={{ color: filtered ? '#1890ff' : undefined }} />
    ),
    onFilter: (value, record) =>
      record[dataIndex]
        .toString()
        .toLowerCase()
        .includes(value.toLowerCase()),
    onFilterDropdownVisibleChange: visible => {
      if (visible) {
        setTimeout(() => this.searchInput.select());
      }
    },
    render: text => (
      <Highlighter
        highlightStyle={{ backgroundColor: '#ffc069', padding: 0 }}
        searchWords={[this.state.searchText]}
        autoEscape
        textToHighlight={text.toString()}
      />
    ),
  });

  handleSearch = (selectedKeys, confirm) => {
    confirm();
    this.setState({ searchText: selectedKeys[0] });
  };

  handleReset = clearFilters => {
    clearFilters();
    this.setState({ searchText: '' });
  };

  render() {
    const columns = [
      {
        title: '用户名',
        dataIndex: 'name',
        width: '50%',
        ...this.getColumnSearchProps('name'),
      },
      {
        title: 'ID',
        dataIndex: '_id',
        width: '50%',
      }
    ];
    return <Table columns={columns} dataSource={data} />;
	
  }
}
